const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const user = require('../../../../services/user.js');
const common = require('../../../../utils/common.js');
const app = getApp()
var _animation; // 动画实体
Page({
  data: {
    //橙贝不足提示开关
    hint: false,
    //结果开关
    prize: false,
    //规则开关
    guize: false,
    //橙贝余额
    cbsum: 0,
    //剩余抽奖次数
    surplus: 10,
    //中奖记录数据
    recordArr: [],
    //给奖品定位，摆放在转盘
    styleArr: [
      "transform: rotate(-21deg);margin-top: 84rpx;margin-left: 200rpx;",
      "transform: rotate(-67deg);margin-top: 174rpx;margin-left: 110rpx;",
      "transform: rotate(-112deg);margin-top: 298rpx;margin-left: 110rpx;",
      "transform: rotate(-158deg);margin-top: 388rpx;margin-left: 200rpx;",
      "transform: rotate(-202deg);margin-top: 388rpx;margin-left: 324rpx;", 
      "transform: rotate(-246deg);margin-top: 298rpx;margin-left: 418rpx;", 
      "transform: rotate(-292deg);margin-top: 174rpx;margin-left: 418rpx;",
      "transform: rotate(-336deg);margin-top: 84rpx;margin-left: 324rpx;"
      ]
  },
  //加载
  onLoad: function () {
    var _this = this
    if (app.globalData.logininfo != null && app.globalData.logininfo != '') {
      _this.setData({
        logininfo: app.globalData.logininfo,
        openid: app.globalData.logininfo.openid,
      });
    }
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        //获取奖池数据
        var data = {};
        data.openid = openid;
        data.access_token = res.access_token;
        data.type = 2;
        util.post(api.GetPrize, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              prizeArr: res.result.list,
              opencj: true
            })
          }
        });
        //获取用户信息
        var data = {};
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.GetUserInfoByHomepage, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              userData: res.result.json,
            })
            _this.setData({
              cbsum: _this.data.userData.enableIntegral
            })
          }
        });
        //获取七天的转盘抽奖记录
        util.post(api.GetLuckyDrawInfo, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              recordArr: res.result.list
            })
            var date = new Date();
            var newD = util.getDateStr(date, 0);
            var arr = res.result.list
            var count = 0
            for(var i=0;i<arr.length;i++){
              if (newD.replace(/-/g, '') == arr[i].createdate.replace(/-/g, '').substring(0, 8)){
                count +=1
              }
            }
            _this.setData({
              surplus: 10 - count
            })
            console.log(_this.data.surplus)
          }
        });
      });
    }
  },
  //点击开始抽奖
  rotateAni: function () {
    var _this = this
    if(_this.data.surplus>0){
      _this.audioCtx = wx.createAudioContext('myAudio')
      var cb = _this.data.cbsum
      if (cb < 10) {
        _this.setData({
          prize: false,
          hint: true,
          rotateAniTitle: "橙贝余额不足噢 ~"
        })
      } else {
        _this.audioCtx.seek(0)
        _this.audioCtx.play()
        _this.close();
        var openid = app.globalData.logininfo.openid;
        var data = {};
        if (openid != null && openid != "") {
          util.post(api.fetchToken()).then(function (res) {
            if (res.access_token != '') {
              return Promise.resolve({
                access_token: res.access_token
              });
            }
          }).then(function (res) {
            data.openid = openid;
            data.access_token = res.access_token;
            util.post(api.LuckyDraw, data).then(function (res) {
              if (res.code == 200) {
                _this.onLoad()
                var result = res.result.obj
                var arr = _this.data.prizeArr
                _this.setData({
                  result: result,
                  result2: res.result.obj2
                })
                var sel = 0
                for (var i = 0; i < arr.length; i++) {
                  if (arr[i].id == result.id) {
                    sel = parseInt(i + 1)
                  }
                }
                _this.initialize();
                setTimeout(function () {
                  _this.openRotate(sel);
                }, 200)
                setTimeout(function () {
                  _this.setData({
                    prize: true
                  })
                }, 6000)
              }
            });
          });
        }
      }
    }else{
      _this.setData({
        prize: false,
        hint: true,
        rotateAniTitle: "今日抽奖次数用完啦 ~"
      })
    }
  },
  //初始化转盘
  initialize: function () {
    var _this = this
    _animation = wx.createAnimation({
      duration: 0,
      timingFunction: 'linear',
    })
    _animation.rotate(0).step()
    _this.setData({
      animation: _animation.export()
    })
  },
  //开启转盘旋转
  openRotate: function (sel) {
    var _this = this
    _this.setData({
      opencj: false
    })
    _animation = wx.createAnimation({
      duration: 5000,
      timingFunction: 'ease',
    })
    _animation.rotate(2880 + (sel * 45) - 21).step()
    _this.setData({
      animation: _animation.export(),
    })
    setTimeout(function () {
      _this.setData({
        opencj: true
      })
    }, 6000)
  },
  //打开活动规则
  openguize: function(){
    var _this = this
    _this.setData({
      guize: true
    })
  },
  //打开中奖记录
  openRecord: function(){
    var _this = this
    _this.setData({
      record: true
    })
  },
  //关闭所有窗口
  close: function(){
    var _this = this
    _this.setData({
      guize: false,
      prize: false,
      hint: false,
      record: false
    })
  },
  //跳转成绩单
  gotodrade: function(){
    app.switchData = 1;
    wx.switchTab({
      url: '/pages/grade/grade',
    });
  },
  //跳转新建圈子
  doPublish: function (event) {
    wx.navigateTo({
      url: '../../../../ucenter/pages/ucenter/personcenter/publish/publish?userid=1',
    })
  },
})